package cn.josh.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.josh.bean.Ec_user;
import cn.josh.service.Ec_userService;
import cn.josh.servicelmpl.Ec_userServiceImpl;

/**
 * Servlet implementation class RegisterAjaxServlet
 */
@WebServlet("/RegisterAjaxServlet")
public class RegisterAjaxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		
		//得到ajax传入的用户名
		String userName = request.getParameter("userName");
		System.out.println("ajax传入的数据是：" + userName);
		
		//将用户名封装
		Ec_user user = new Ec_user();
		user.setLogin_name(userName);
		
		//连接数据库服务
		Ec_userService ec_us = new Ec_userServiceImpl();
		Ec_user backUser = ec_us.login(user);
		
		//将数据反馈给前端
		PrintWriter pw = response.getWriter();
		
		if(backUser != null)
		{
			System.out.println("用户已经存在");
			pw.write("true");
		}
		else
		{
			System.out.println("用户可以注册");
			pw.write("false");
		}
		
		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
